﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Adex;
using MediaMonitor.Common;

namespace MediaMonitor
{
    public partial class Get_Thumb : Adex.BasePage
    {
        /// <summary>
        /// f
        /// w
        /// h
        /// </summary>
        /// <remarks></remarks>

        private string f;
        private int w;
        private int h;

        protected void Page_Load(object sender, EventArgs e)
        {
            f = Utility.GetRequestedVal("f", "");
            w = Convert.ToInt32(Utility.GetRequestedVal("w", "96"));
            h = Convert.ToInt32(Utility.GetRequestedVal("h", "82"));


            int intRatioHeight = 0;
            //Ratio Height
            int intRatioWidth = 0;
            //Ratio Width
            //get the file name -- fall800.jpg
            string File = f;
            //File = "/images/" & Request.QueryString("s").Replace("http://", "").Replace("https://", "").Replace("www.", "") & "/" & Request.QueryString("f")

            System.Drawing.Image Image = null;
            try
            {
                //create an image object, using the filename we just retrieved
                Image = System.Drawing.Image.FromFile(Server.MapPath(File), true);


                //Set Ratio height and width

                intRatioWidth = w;
                intRatioHeight = h;


                ImageRatio(Image, ref intRatioWidth, ref intRatioHeight);

                //create the actual thumbnail image
                System.Drawing.Image ThumbnailImage = Image.GetThumbnailImage(intRatioWidth, intRatioHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

                //make a memory stream to work with the image bytes
                System.IO.MemoryStream ImageStream = new System.IO.MemoryStream();

                //put the image into the memory stream
                ThumbnailImage.Save(ImageStream, System.Drawing.Imaging.ImageFormat.Bmp);

                //make byte array the same size as the image
                byte[] ImageContent = new byte[ImageStream.Length + 1];

                //rewind the memory stream
                ImageStream.Position = 0;

                //load the byte array with the image
                ImageStream.Read(ImageContent, 0, (int)ImageStream.Length);

                //return byte array to caller with image type

                Response.ContentType = "image/gif";
                Response.BinaryWrite(ImageContent);


                ImageStream.Flush();
                ImageStream.Dispose();
                ThumbnailImage.Dispose();
                Image.Dispose();

            }
            catch 
            {
                //throw ex;
            }

        }

        public bool ThumbnailCallback()
        {
            return true;
        }


        protected void ImageRatio(System.Drawing.Image img, ref int Width, ref int Height)
        {
            //If img.Width > Width Or img.Height > Height Then
            //    Dim widthRatio As Double = CType(img.Width, Double) / CType(Width, Double)
            //    Dim heightRatio As Double = CType(img.Height, Double) / CType(Height, Double)
            //    Dim ratio As Decimal = Math.Max(widthRatio, heightRatio)
            //    Width = CType(img.Width / ratio, Integer)
            //    Height = CType(img.Height / ratio, Integer)
            //End If

            if (img.Width < Width & img.Height < Height)
            {
                Width = img.Width;
                Height = img.Height;
                return;
            }


            if (img.Width > Width & img.Height > Height)
            {
                double widthRatio = Convert.ToDouble(img.Width) / Convert.ToDouble(Width);
                double heightRatio = Convert.ToDouble(img.Height) / Convert.ToDouble(Height);
                decimal ratio = Math.Max((byte)widthRatio, (byte)heightRatio);
                Width = Convert.ToInt32(img.Width / ratio);
                Height = Convert.ToInt32(img.Height / ratio);
                return;
            }


            if (img.Width > Width & img.Height <= Height)
            {
                double widthRatio = Convert.ToDouble(img.Width) / Convert.ToDouble(Width);
                double heightRatio = Convert.ToDouble(img.Height) / Convert.ToDouble(img.Height);
                decimal ratio = Math.Max((byte)widthRatio, (byte)heightRatio);
                Width = Convert.ToInt32(img.Width / ratio);
                Height = Convert.ToInt32(img.Height / ratio);
                return;
            }


            if (img.Width <= Width & img.Height > Height)
            {
                double widthRatio = Convert.ToDouble(img.Width) / Convert.ToDouble(img.Width);
                double heightRatio = Convert.ToDouble(img.Height) / Convert.ToDouble(Height);
                decimal ratio = Math.Max((byte)widthRatio, (byte)heightRatio);
                Width = Convert.ToInt32(img.Width / ratio);
                Height = Convert.ToInt32(img.Height / ratio);
                return;
            }



        }


    }
}